#include<iostream>
#include<algorithm>
using namespace std;
class cat {
private:
	char name[30];
	int weight;
public:
	int get() const {
		return weight;
	}
	void input() {
		cin >> name >> weight;
	}
	void output()const
	{
		cout << name;
	}
};
bool cmp(const cat& a, const cat& b) {
	return a.get() < b.get();
}
int main()
{
	int t;
	cin >> t;
	cat* arr = new cat[t];
	for (int i = 0; i < t; i++) {
		arr[i].input();
	}
	sort(arr, arr + t, cmp);
	for (int i = 0; i < t; i++) {
		if (i == t - 1) {
			arr[i].output();
			cout << endl;
		}
		else {
			arr->output();
			cout << " ";
		}
	}
	delete[] arr;
	return 0;
}